package com.boc.langxiang.presenter.common;


import com.boc.langxiang.contract.common.ForgetPasswordContract;
import com.boc.langxiang.entity.request.FindPwdRequest;
import com.boc.langxiang.entity.request.SmsCodeRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.SmsCodeEntity;
import com.boc.langxiang.entity.response.UserInfo;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;
import com.boc.langxiang.net.ResultException;
import com.boc.langxiang.util.L;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CreateTime: 2016-06-14上午10:40
 * Author: wjkjinke00@126.com
 * Description:
 */
public class ForgetPasswordPresenter implements ForgetPasswordContract.Presenter {

    private final ApiService mService;
    private ForgetPasswordContract.View mView;

    public ForgetPasswordPresenter(ForgetPasswordContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        mView = view;
    }

    @Override
    public void sendCode(String phone) {
        SmsCodeRequest request = new SmsCodeRequest();
        request.phone = phone;
        Observable<BaseResponse<SmsCodeEntity>> register = mService.accountFindCode(request.params());
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<SmsCodeEntity>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<SmsCodeEntity> response) {
                        SmsCodeEntity smsCodeEntity = response.getContent();
                        mView.onGetSmsCodeSuccess(smsCodeEntity);
                    }
                });
    }

    @Override
    public void accountFindPwd(String phone, String pwd1, String pwd2) {
        mView.showLoading();
        FindPwdRequest request = new FindPwdRequest();
        request.phone = phone;
        request.pwd = pwd1;
        request.pwdconf = pwd2;
        Observable<BaseResponse<UserInfo>> register = mService.accountFindPwd(request.params());
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<UserInfo>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<UserInfo> response) {
                        UserInfo userInfo = response.getContent();
                        UserInfoManager.getInstance().setUserInfo(userInfo);
                        mView.loginSuccess();
                    }
                });
    }

    @Override
    public void smsCodeVerify(String phone, String smscode) {
        mService.smsCodeVerify(phone, smscode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.smsCodeVerifySuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ResultException) {
                            ResultException resultException = (ResultException) e;
                            L.i(resultException.toString());
                            mView.smsCodeVerifyFail(resultException.getCode(), resultException.getMessage());
                        } else {
                            super.onError(e);
                        }

                    }
                });
    }

}
